// pages/aiCreationImg/aiCreationImg.js
import ElecSeal from '../../utils/ElecSealSmall.js'

const app = getApp()
let reqJs = require('../../utils/req.js')
const id35 = 35 // 抽奖id
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        cluesCurrent: '', //判断是不是游玩页面跳转过来的  
        mapdataItem: {

        },
        reqData: "",

        itemFlag: false, //是否打卡了
        luckDrawItem: false, //抽中的礼品对象

    },
    // 计算类名的方法
    getBgClass() {
        const id = this.data.mapdataItem.id;
        if (id === 30) return 'PageBgImg30';
        if (id === 31) return 'PageBgImg31';
        return 'PageBgImg36';
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        if (options.id) {

            let mapdataItem = wx.getStorageSync('allPlayProjects').experience.find(item => item.id == options.id);

            this.setData({
                mapdataItem,
            })
            // 更新数据时计算并设置类名
            this.setData({
                bgClass: this.getBgClass()
            });
        } else {
            wx.showToast({
                title: '网络服务出错，请重新进入小程序',
                icon: 'none',
                duration: 2000
            })
        }


    },
    onUploadSuccess(e) {
        console.log('图片上传成功', e.detail.url);
        // 可以在这里处理上传成功后的逻辑

        

    },
    // 后门
    BackDoorBind() {
        this.signIn()
    },
    // 打卡
    signIn() {
        var that = this
        console.log('>>>>>')
        reqJs.playGame(this.data.mapdataItem.id).then(res => {
            console.log('>>>>>', res)
            if (res.code != 200) {
                return
            }
            that.setData({
                itemFlag: true,
            })
            wx.showToast({
                title: '打卡成功',
            })

            this.upload()

            console.log(this.data)

        }).catch(error => {
            console.log(error)
        })
    },
    // 抽奖
    luckDraw() {
        var that = this
        console.log('>>>>>')
        if (this.data.luckDrawItem && this.data.luckDrawItem.gid) {

            wx.setStorageSync('gItem', this.data.luckDrawItem)
            wx.navigateTo({
                url: '/pages/aiCreationImg2/aiCreationImg2?id=' + this.data.luckDrawItem.gid,
            })

            return
        }
        reqJs.luckDraw(id35).then(res => {
            console.log('>>>>>', res)
            if (res.code == 200) {
                wx.showToast({
                    title: '抽奖成功',
                })

                wx.setStorageSync('gItem', res.data)

                wx.navigateTo({
                    url: '/pages/aiCreationImg2/aiCreationImg2?id=' + res.data.gid,
                })
            }


            console.log(this.data)

        }).catch(error => {
            console.log(error)
        })
    },
    // 盖章
    touchstart(e) {
        // 是否已经被核销 
        if (this.data.mapdataItem.status == 1) {

            return

        }
        // 测试
        // this.signIn()

        // console.log(e)
        var that = this
        ElecSeal.touchStart(e, function (sealCode, SealCode) {
                console.log(sealCode)
                switch (sealCode) {
                    case '1234SEAL9':
                        that.signIn()
                        break;
                    case '123SE6AL9':
                        that.signIn()
                        break;
                    case '1S3EA6L89':
                        that.signIn()
                        break;
                    case '1S34EAL89':
                        that.signIn()
                        break;
                    case 'fail':
                        // that.setData({
                        //   showInputCode: true
                        // })
                        break;
                    case 'false':
                        // that.setData({
                        //   showInputCode: true
                        // })
                        break;
                    default:
                        // that.setData({
                        //   showInputCode: true,
                        // })
                }
            },
            // function () {
            //   that.setData({
            //     taking_failed: true,
            //     showInputCode: false,
            //   })
            // }
        )

    },
    bindload() {
        wx.hideLoading()
    },
    imageTap(e) {
        let reqData = this.data.reqData
        wx.previewImage({
            current: reqData, // 当前显示图片的 http 链接
            urls: [reqData] // 需要预览的图片 http 链接列表
        })
    },
    //跳转到任务线页面
    goXmgame() {
        wx.reLaunch({
            url: '/packageL/pages/searchLoss/searchLoss',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {


    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

        this.upload()
    },
    upload() {
        var that = this

        app.getUser().then((res) => {
            console.log(res) //用户信息
            let reqCheckData = res.data

            const itemFlag = res.data.play_list.some(item => item.pid == this.data.mapdataItem.id)




            const luckDrawItem = res.data.play_list.find(item => item.pid == id35) ? res.data.play_list.find(item => item.pid == id35) : false

            if (luckDrawItem) {
                wx.setStorageSync('gItem', luckDrawItem)
            }

            that.setData({
                itemFlag,
                reqCheckData,
                luckDrawItem,
            })


        })
        this.getProjectList()


    },
    isSuccessMessageVisible() {
        this.setData({
            isSuccessMessageVisible: true
        })
        setTimeout(() => {
            this.setData({
                isSuccessMessageVisible: false
            });
        }, 2000);
    },
    getProjectList() {
        // 更新用户列表数据列表数据
        reqJs.getProjectList().then(res => {

            console.log('>>>>>', res)
            let item35 = res.data.find(item => item.id == id35)
            this.setData({
                item35,
            })
        }).catch(error => {
            console.log(error)
        })
    },
    copyText() {
        // 调用微信复制API
        wx.setClipboardData({
            data: "在长春车展偶遇红旗 “隐藏惊喜”！东方 豪华质感拉满，红旗豪华气场与孙颖莎的 拼搏劲儿完美呼应。",
            success: () => {
                // 复制成功提示
                wx.showToast({
                    title: '复制成功',
                    icon: 'success',
                    duration: 2000
                });
            },
            fail: (err) => {
                // 复制失败提示
                wx.showToast({
                    title: '复制失败，请重试',
                    icon: 'none',
                    duration: 2000
                });
                console.error('复制失败:', err);
            }
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },


})